package org.haredot.controller;

import org.haredot.annotation.LogModule;
import org.haredot.entity.Btn;
import org.haredot.service.BtnService;
import org.haredot.vo.PageVo;
import org.haredot.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@LogModule("按钮管理")
@RestController
@RequestMapping("/btn")
public class BtnController {
    
    @Resource
    private BtnService btnService ;

    @PostMapping("/{funcId}")
    public ResultVo<?> saveBtn(@PathVariable("funcId") Long funcId, @RequestBody Btn btn) {
        
        return btnService.saveBtn(funcId, btn);
    }

    @GetMapping("/list{funcId}")
    public ResultVo<?> list(@PathVariable("funcId") Long funcId,
                            @RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
                            @RequestParam(value = "limits", required = false, defaultValue = "10")Integer limits) {

        PageVo<Btn> pageVo = new PageVo<>(page, limits) ;

        return btnService.queryBtnsByFuncId(funcId, pageVo);
    }

    @PutMapping("/modify/{btnId}")
    public ResultVo<?> modifyBtnStatus(@PathVariable("btnId")Long btnId, @RequestParam Integer status) {

        Integer target = status != 0 ? 0 : 1 ;
        // 构建请求参数
        Map<String, ? extends Number> stringHashMap = new HashMap<>(Map.of("btnId", btnId, "status", status, "target", target));

        return btnService.modifyStatus(stringHashMap);
    }

    @DeleteMapping("/remove/{btnId}")
    public ResultVo<?> removeBtn(@PathVariable("btnId")Long btnId) {

        return btnService.removeBtn(btnId);
    }
}
